<?php
include_once(JL_ROOT.'/jl_controller.cls.php');
include_once(JL_ROOT.'/jl_tools.cls.php');

class UserDo extends JL_controller {
	
	public function __init(){
		$this->page = JL_factory::get_JL_page();
		
		
	}
	
	public function do__(){

		$this->do__login();
		/*
		$this->page->set_error('error','一个错误测试');
		$this->page->display('/user/error.tpl.php');
		*/
		
	}

	public function do__reg(){
		$this->page->display('user/reg.tpl.php');

	}


	public function do__reg_do(){
	
		$login_name = $_REQUEST['login_name'];

		$login_password = $_REQUEST['login_password'];
		$name = $_REQUEST['user_name'];
		
		$user_a = array('user_login_name'        =>    $login_name,
				'user_login_password'   =>    md5($login_password),
				'user_name'              =>    $name,
				'user_reg_time'          =>    time(),
				'user_reg_ip'            =>    'ip',
				'user_email'             =>    $email,
				);
		$user_temp = $this->func_get_user($login_name);
//		var_dump($user_temp);exit;
		if(empty($user_temp)){
			$new_user_id = $this->func_reg($user_a);//注册
			$this->func_set_tag($new_user_id,$_REQUEST['tag']);

			
			
echo <<<EOF
			<html>
			<head>
			<script language="javascript"> 
				alert('注册成功！现在就可以登陆。');
				window.location='/';
			</script>
			</head>
			<body>
			</body>
			</html>
EOF;
		}else{
			$this->page->set_error('error_title','您的注册存在错误');
			$this->page->set_error('error','登陆名已经存在');
			$this->page->display('/system/error.tpl.php');exit;
		}
		
	}

	public function do__login(){
		
		$this->page->display('/user/login.tpl.php');


	}


	public function do__login_do(){
		$login_name = $_REQUEST['login_name'];
		$login_password = $_REQUEST['login_password'];
		$user = $this->func_get_user($login_name);
		if($user['user_login_password']==md5($login_password)){//如果密码正确
			$user['tag'] = $this->func_get_tag($user['user_id']);
			JL_tools::set_session('me',$user);
			$this->func_update_last_ip_time($user['user_id']);

			$prev_url = JL_tools::get_session('prev_url');
			header('location: '.JL_tools::pick_value($prev_url,'/'));exit;
		}else{
			$this->page->set_error('error_title','您的登陆存在错误');
			$this->page->set_error('error','用户名或密码不正确');
			$this->page->display('user/reg.tpl.php');
		}

	}



	//设置某用户标签
	public function func_set_tag($user_id,$tag_arr_or_tag_word){
		$tag = '';
		if(is_array($tag_arr_or_tag_word)){
			$tag = join(',',$tag_arr_or_tag_word);

		}else{
			$tag = $tag_arr_or_tag_word;
		}

		$table = JL_factory::get_JL_mysql_table('jl_user_tag');
		$table->new_rows(array('user_tag_uid'=>$user_id,'user_tag_content'=>$tag));
		$table->exec();

	}


	//获得某用户标签
	public function func_get_tag($user_id){

		$table = JL_factory::get_JL_mysql_table('jl_user_tag');
		$table->get_rows('*'," WHERE user_tag_uid = {$user_id}");
//		var_dump($table->get_sql());exit;
		$tag_temp = mysql_fetch_assoc($table->exec());

		return $tag_temp['user_tag_content'];

	}




	/*用户注册*/
	public function func_reg($user_data){

		$table = JL_factory::get_JL_mysql_table('jl_user');
		$table -> new_rows($user_data);
		$table -> exec();
		
		return mysql_insert_id();

	}
	
	/*查询用户*/
	public function func_get_user($user_id_or_loginname){

		$table = JL_factory::get_JL_mysql_table();
		$user_id_or_loginname = mysql_escape_string($user_id_or_loginname);
		$sql = "SELECT * FROM jl_user WHERE user_id='{$user_id_or_loginname}' UNION SELECT * FROM jl_user WHERE user_login_name='{$user_id_or_loginname}'";
		$table -> query($sql);

//		var_dump($sql);var_dump($table->exec());exit;
		return mysql_fetch_assoc($table -> exec());


	}
	
	/*根据条件查询用户*/
	public function func_get_user_where($where){

		$table = JL_factory::get_JL_mysql_table('jl_user');
		$user_id_or_loginname = mysql_escape_string($user_id_or_loginname);
		$sql = "SELECT * FROM jl_user WHERE {$where}";
		$table -> query($sql);
		return mysql_fetch_assoc($table -> exec());


		

	}
	
	//修改用户最近登陆时间和IP
	public function func_update_last_ip_time($user_id){

		$table = JL_factory::get_JL_mysql_table('jl_user');
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = time();
		$a = array('user_last_login_ip'=>$ip,'user_last_login_time'=>$time);
		$table->set_rows($a," user_id={$user_id}");
		$table->exec();
		return $a;
	}

	
/*	
	
	public function do__user_reg(){
		$uname = trim($_REQUEST['u']);
		$pwd = $_REQUEST['p'];
		$pwd2 = $_REQUEST['p2'];
		$email = $_REQUEST['email'];
		$errors = null;
		
		
		if(strlen(trim($uname))<6){
			$errors .= '用户名不能少于6个字符<br />';
		}
		if($this->func_get_user_info(trim($uname))){
			$errors .= '用户名已经存在<br />';
		}
		if(strlen($pwd)<6){
			$errors .= '密码不能少于6个字符<br />';
		}
		if($pwd!==$pwd2){
			$errors .= '两次密码输入不一致<br />';
			
		}
		if(strlen($email)<1){
			$errors .= 'email不能为空<br />';
		}
		if(!empty($errors)){
			$this->page->set_error('error',$errors);
			$this->page->display('/user/error.tpl.php');exit;
		}
		
		
		$a = array(
						'user_login_name'        => $uname,
						'user_login_password'    => md5($pwd),
						'user_login_email'       => $email,
							);
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->new_rows($a);
		
		$table->exec();
		
		$this->page->set_item('message','注册成功');
		$this->page->set_item('go_url','/');
		$this->page->display('/user/message.tpl.php');exit;
		
		
		
		
		
	}
	
	
	public function do__login(){
		$uname = $_REQUEST['u'];
		$pwd = $_REQUEST['p'];
		$url = $_REQUEST['url'];
		if($this->func_login($uname,$pwd)){
			
			
			if(!empty($url)){
				header('location:'.$url);exit;
			}else{
				echo '显示消息:登陆成功';
				
			}
			
		}else{
			echo '提示错误:密码错误';
		}
		
	}
	
	//登陆方法
	public function func_login($uname,$pwd){
		$u_info = $this->func_get_user_info($uname);
		
		if($u_info['user_login_password']==md5($pwd)){
			JL_tools::set_session('login_user',$u_info);

			return true;
		}else{
			
			return false;
		}
	}
	
	//得到用户信息
	public function func_get_user_info($uname){
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->get_rows('*',"WHERE user_login_name='{$uname}'");
		
		return mysql_fetch_assoc($table->exec());
		
		

		
	}
	
	
	*/
	
}















?>
